#!/bin/sh
#package:odpkg
#desc:Install package
#type:local

# Copyright(c) 2012 OpenDomo Services SL. Licensed under GPL v3 or later

# Este script recibira el nombre del paquete a instalar a peticion del usuario.
# El nombre debe encontrarse dentro de la lista "platformavailable.txt", que 
# habremos generado con el comando "updatePackageList" (sin argumentos). 
# Entonces, el script buscará el primer elemento que coincida con el nombre
# indicado, lo intentará descargar y si lo consigue, lanzará el instalador.

# Caso 2: si este script es llamado sin argumentos, se procederá a mostrar la 
# lista de paquetes a instalar

PKGLIST="/tmp/platformavailable.txt"
URLLIST="/tmp/urlavailable.txt"
PKGLIST="/tmp/allavailable.txt"
PKGSTORAGE="/mnt/odconf/pkgcache"


# If the package list is not created, launch updatePackageList process, and fallback
if ! test -f "$PKGLIST"; then
	echo "#LOADING Checking available packages"
	echo
	/usr/local/opendomo/updatePackageList.sh >/dev/null
fi

# If there's no first argument specified, display a list with all available packages
PACKAGE_SELECTED=0
if test -n "$1"; then
	PACKAGE_SELECTED=1
	
	if ! test -d $PKGSTORAGE/; then
		echo "#WARN: [$PKGSTORAGE] does not exist"
		echo "#WARN: Installed packages will be lost after restart!"
		PKGSTORAGE="/tmp/"
	fi
	
   for pkgname in $@; do
	if test -f /var/pkg/installed/$pkgname; then
		echo "#ERR: Package [$pkgname] already installed!"
	else
		# Se encuentra en la lista de URLS?
		if grep -q "/$pkgname-" $URLLIST; then
			DEPS=`grep "^$pkgname" $PKGLIST | uniq | cut -f2 -d';' | sed 's/,/ /g'`
			URL=`grep "^$pkgname" $URLLIST | uniq | cut -f2 -d';'`
			#echo "DEPENDENCIAS: $DEPS"
		 	for dep in $DEPS; do
		 		if ! test -f /var/pkg/installed/$dep; then
		 			echo "#ERR Package requires [$dep], not installed"
		 			exit 1
		 		fi
		 	
		 	done
		 	
		 	if wget --spider -q "$URL" 2>/dev/null
		 	then	
				echo "#INF: Downloading [$pkgname], please wait"
				echo 
				echo
				cd $PKGSTORAGE
				wget -q $URL > /tmp/downloading.tml
			 	BN=`basename $URL` 
			 	if ! test -f $PKGSTORAGE/$BN; then
			 		echo "#ERR File could not be downloaded"
			 		exit 2
			 	fi
			 	
			 	echo "00000000" > /var/pkg/installed/$pkgname
			 	
			 	if pkg_add $BN 2>/dev/null >/dev/null
			 	then
			 		echo "# Package installed sucessfully"
			 		/usr/local/opendomo/discovery/processServices.sh &
			 		echo 
			 	else
			 		echo "#WARN The system must be restarted"
			 	fi
			else
				echo "#ERR: Package not found in repository"
				exit 3
			fi
			# Language files are not valid anymore until they are updated
			#rm /etc/opendomo/langfiles/*
 		else
			echo "#ERR: Package [$pkgname] not found"
			exit 1
		fi
	fi
   done
#else
#	echo "# Choose the package you want to install"
fi
PREV="-"
PREF=""
echo "#> Available packages"
echo "list:`basename $0`	selectable detailed"
# El "grep" lo hacemos para ocultar los paquetes de sistema, prefijados
grep -v "^_" $PKGLIST | cut -f 1 -d'-' > /tmp/lstpkg.txt
#ls /var/pkg/installed >> /tmp/lstpkg.txt
for i in `sort /tmp/lstpkg.txt | uniq`; do

	if test -f /var/pkg/installed/$i; then
		state="installed"
	else
		state="available"
	fi

	PREF=`echo $i | cut -f 1 -d'_'`
	if test "$PREV" = "$PREF"; then
		TYPE="sub package"
		if test "$lpni" -eq 1; then
			state="unavailable"
		fi
	else
		TYPE="package"
		if test "$state" = "available"; then
			lpni=1;
		else
			lpni=0;
		fi
		PREV="$i"
	fi

	name="$i"
	fdesc="$i"
	if test -f /tmp/$i.info; then
		name=`cat /tmp/$i.info | head -n 1`
		fdesc=`head -n2 /tmp/$i.info | tail -n1`
	else
		if test -f /var/pkg/$i.desc ; then
			name=`cat /var/pkg/$i.desc | head -n 1`
			fdesc=`head -n2 /var/pkg/$i.desc | tail -n1`		
		fi
	fi
	if test -z "$name"; then
		name="$i"
	fi
	if test -z "$fdesc"; then
		fdesc=$name
	fi
	
	echo -e "\t$i\t$name\t$TYPE $state\t$fdesc"
done
rm -fr /tmp/lstpkg.txt 2>/dev/null

echo "actions:"
echo "	installPackage.sh	Install selected packages"
echo 

